﻿using mvcForum.Core;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Interfaces.Services;
using mvcForum.Web.Controllers;
using System;
using System.Linq;
using System.Net.Mail;
using System.Web.Mvc;

namespace mvcForum.Web.Areas.ForumAPI.Controllers {

	public class TestMailController : BaseAPIController {
		private readonly IMailService mailService;
		private readonly IConfiguration config;

		public TestMailController(IMailService mailService, IConfiguration config, ILogger logger)
			: base() {

			this.config = config;
			this.mailService = mailService;
		}

		[HttpPost]
		[Authorize(Roles = Constants.Roles.Combined)]
		public ActionResult Send(String recipient, String mode) {
			MailAddress address = null;
			try {
				address = new MailAddress(recipient);
			}
			catch {
				return new HttpStatusCodeResult(500, "Invalid e-mail address");
			}

			try {
				this.mailService.Send(
					new MailAddress(this.config.RobotEmailAddress, this.config.RobotName),
					new MailAddress[] { address }.ToList(),
					"Test e-mail",
					"This is a test e-mail");
			}
			catch (Exception ex) {
				return new HttpStatusCodeResult(500, ex.Message);
			}

			return new HttpStatusCodeResult(200);
		}

	}
}